
/*头文件声明区*/
#include <REGX52.H>
#include <INTRINS.H>
/*变量声明区域*/
unsigned char Key_Val,Key_Down,Key_Up,Key_Old;
unsigned char Time;
unsigned char ucLed1=0xfe;
unsigned char ucLed2=0x7f;
unsigned char ucLed11=0xef;
unsigned char ucLed21=0xf7;
unsigned char ucLed3;
unsigned char Mo=1;//控制模式的变量
bit System;//控制系统启动


/*Delay函数*/    //注意要用int形式
void Delay(unsigned int xms)		//@12.000MHz
{
	while(xms--)
	{
		unsigned char i, j;

		i = 2;
		j = 239;
		do
		{
			while (--j);
		} while (--i);
	}
}

/*按键读取函数*/
unsigned char Key_Read()
{
	unsigned char temp =0;
	P3_0=0;P3_1=1;P3_2=1;P3_3=1;
	if(P3_4==0) temp=1;
	if(P3_5==0) temp=2;
	if(P3_6==0) temp=3;
	if(P3_7==0) temp=4;
	P3_0=1;P3_1=0;P3_2=1;P3_3=1;
	if(P3_4==0) temp=5;
	if(P3_5==0) temp=6;
	if(P3_6==0) temp=7;
	if(P3_7==0) temp=8;
	P3_0=1;P3_1=1;P3_2=0;P3_3=1;
	if(P3_4==0) temp=9;
	if(P3_5==0) temp=10;
	if(P3_6==0) temp=11;
	if(P3_7==0) temp=12;
	P3_0=1;P3_1=1;P3_2=1;P3_3=0;
	if(P3_4==0) temp=13;
	if(P3_5==0) temp=14;
	if(P3_6==0) temp=15;
	if(P3_7==0) temp=16;
	return temp;
}

/*模式一*/
void Mo_1()
{
	
	ucLed1=_crol_(ucLed1,1);
	P1 = ucLed1;
	Delay(200);
}

/*模式二*/
void Mo_2()
{
	ucLed2=_cror_(ucLed2,1);
	P1 = ucLed2;
	Delay(200);
}

/*模式三*/
void Mo_3()
{
	ucLed3 = ucLed1&ucLed2;
	P1 = ucLed3;
	ucLed1=_crol_(ucLed1,1);
	ucLed2=_cror_(ucLed2,1);
	if(ucLed3==0xe7)
	{
		ucLed1=0xfe;
		ucLed2=0x7f;
	}
	Delay(300);
}

/*模式四*/
void Mo_4()
{
	ucLed3 = ucLed11&ucLed21;
	P1 = ucLed3;
	ucLed11=_crol_(ucLed11,1);
	ucLed21=_cror_(ucLed21,1);
	
	if(ucLed3==0x7e)
	{
		ucLed11=0xef;
		ucLed21=0xf7;
	}
	Delay(300);
}

/*Main*/
void main()
{
	while(1)
	{
		Key_Val = Key_Read();//读取键码值
		Key_Down = Key_Val&(Key_Val^Key_Old);//检测下降沿
		Key_Up = ~Key_Val&(Key_Val^Key_Old);//检测上升沿
		Key_Old = Key_Val;//扫描辅助变量
		switch(Key_Down)
		{
			case 1:
				System=1;
			break;
			case 2:
				System=0;
			break;
			case 3:
				Mo++;
			break;
			case 4:
				Mo--;
			break;
		}
		if(Mo==5)Mo=1;
		if(Mo==0)Mo=4;
		if(System==1)
		{
			switch(Mo)
			{
				case 1:
					Mo_1();
				break;
				case 2:
					Mo_2();
				break;
				case 3:
					Mo_3();
				break;
				case 4:
					Mo_4();
				break;
			}
		}
	}
}

